import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * ClassName:MyPanel
 * Description:
 * author: jiuyang
 * DateTime: 2024/8/7 上午11:19
 */
public class MyPanel extends JPanel implements KeyListener, ActionListener {

    ImageIcon headImage_top = new ImageIcon("D:\\TJY\\代码\\Game\\RetroSnaker\\image\\tou_top.png");
    ImageIcon headImage_bottom = new ImageIcon("D:\\TJY\\代码\\Game\\RetroSnaker\\image\\tou_bottom.png");
    ImageIcon headImage_left = new ImageIcon("D:\\TJY\\代码\\Game\\RetroSnaker\\image\\tou_left.png");
    ImageIcon headImage_right = new ImageIcon("D:\\TJY\\代码\\Game\\RetroSnaker\\image\\tou_right.png");
    ImageIcon shengti = new ImageIcon("D:\\TJY\\代码\\Game\\RetroSnaker\\image\\shengti.png");

    public MyPanel() {

        snakeX[0] = 100;
        snakeY[0] = 100;

        snakeX[1] = 75;
        snakeY[1] = 100;

        snakeX[2] = 50;
        snakeY[2] = 100;


        footX = 25 + 25 * random.nextInt(20);
        footY = 25 + 25 * random.nextInt(20);

        this.setFocusable(true);
        this.addKeyListener(this);

        timer.start();


    }

    int len = 3;

    int[] snakeX = new int[784];
    int[] snakeY = new int[784];

    Direction direction = Direction.right;

    boolean isStart = false;

    Timer timer = new Timer(100, this);

    int footX;
    int footY;

    Random random = new Random();
    ImageIcon footImage = new ImageIcon("D:\\TJY\\代码\\Game\\RetroSnaker\\image\\shengti.png");

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        this.setBackground(Color.red);

        g.fillRect(0, 0, 700, 700);

        switch (direction){
            case top -> headImage_top.paintIcon(this, g, snakeX[0], snakeY[0]);
            case bottom -> headImage_bottom.paintIcon(this, g, snakeX[0], snakeY[0]);
            case left -> headImage_left.paintIcon(this, g, snakeX[0], snakeY[0]);
            case right -> headImage_right.paintIcon(this, g, snakeX[0], snakeY[0]);
        }

        for (int i = 1; i < len; i++) {
            shengti.paintIcon(this, g, snakeX[i], snakeY[i]);
        }

        if (!isStart) {
            g.setColor(Color.white);
            g.setFont(new Font("宋体", Font.BOLD, 50));
            g.drawString("请按空格开始游戏！", 50, 300);
        }

        footImage.paintIcon(this, g, footX, footY);


    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == 32) {
            isStart = !isStart;
            repaint();
        }
        switch (keyCode) {
            case 87:
                direction = Direction.top;
                break;
            case 83:
                direction = Direction.bottom;
                break;
            case 65:
                direction = Direction.left;
                break;
            case 68:
                direction = Direction.right;
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart) {
            for (int i = len - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }
            switch (direction) {
                case top -> {
                    snakeY[0] -= 25;
                    if (snakeY[0] < 0) {
                        snakeY[0] = 700;
                    }
                }
                case bottom -> {
                    snakeY[0] += 25;
                    if (snakeY[0] > 700) {
                        snakeY[0] = 0;
                    }
                }
                case left -> {
                    snakeX[0] -= 25;
                    if (snakeX[0] < 0) {
                        snakeX[0] = 700;
                    }
                }
                case right -> {
                    snakeX[0] += 25;
                    if (snakeX[0] > 700) {
                        snakeX[0] = 0;
                    }
                }
            }
            if (snakeX[0] == footX && snakeY[0] == footY){
                len++;
                footX = 25 + 25 * random.nextInt(20);
                footY = 25 + 25 * random.nextInt(20);
            }
            repaint();
            timer.start();
        }
    }
}
